/// <reference path="stage.ts" />
//declare var createjs;

class Player {
    // orientation
    static STAND = 0;
    static MOVE_RIGHT = 1;
    static MOVE_LEFT = 2;
    static MOVE_UP = 4;
    static MOVE_DOWN = 8;
    static MOVE_JUMP = 16;

    //mouvement speed
    static WALK = 1;
    static RUN = 2;

    static WALKING_SPEED_CONFIG = 2;
    static RUNNING_SPEED_CONFIG = 4;
    //static STAND = 3;

    //jump properties
    v_x = 1.5;
    v_y = -4;
    v_grav = 0.16;
    v_saut = -4;
    original_y; //cheat

    //stage_manager: StageManager;
    player_img = "../assets/img/entities/maxi_bobo.png";
    sprite;
    speed_mouvement: number = Player.RUN;
    orientation: number = Player.MOVE_RIGHT;
    is_dead: boolean = false;
    is_jumping: boolean = false;
    is_running: boolean = false;//not use yet


    constructor(stage_manager: StageManager, x:number, y:number) {
        console.log("< -- >");
        //this.stage_manager = stage_manager;
        var sprite_sheet = new createjs.SpriteSheet({
            images: [this.player_img],
            frames: [
                // x, y, width, height, imageIndex,regX,regY
                //walk
                [0, 0, 29, 27, 0, 15, 14],
                [29, 0, 29, 27, 0, 15, 14],
                [58, 0, 29, 27, 0, 15, 14],
                //run
                [0, 27, 38, 27, 0, 19, 14],
                [38, 27, 38, 27, 0, 19, 14],
                [76, 27, 38, 27, 0, 19, 14],
                [114, 27, 38, 27, 0, 19, 14],

            ],//*/

            animations: {
                walk: [0, 2, "walk", 0.3],
                run: [3, 5, "run", 0.6],
                stand: [1, 1, "stand", 0.3]
            }
        });

        this.sprite = new createjs.Sprite(sprite_sheet);
        this.sprite.gotoAndPlay("walk");//animate
        this.sprite.shadow = new createjs.Shadow("#454", 0, 2, 1); //bobo shadow 
        this.sprite.name = "player";
        this.sprite.direction = 90;
        this.sprite.vX = 4;
        this.sprite.x = x;
        this.sprite.y = y;
        this.sprite.currentFrame = 0;

    }

    runAnimation() {
        if (this.is_dead) return;

        this.speed_mouvement = Player.RUN;
        this.sprite.gotoAndPlay("run");
    }


    standAnimation() {
        this.speed_mouvement = Player.STAND;
        this.sprite.gotoAndPlay("stand");
    }

    walkAnimation() {
        if (this.is_dead) return;

        this.speed_mouvement = Player.WALK;
        this.sprite.gotoAndPlay("walk");
    }

    deathAnimation() {
        this.is_dead = true;
        this.standAnimation();
        this.orientation = Player.MOVE_UP;
    }

    jump() {
        if (this.is_jumping) return;

        this.original_y = this.sprite.y;
        this.is_jumping = true;
        this.v_x = 0;//1
        this.v_y = -4;
        this.v_grav = 0.16;
        this.v_saut = -4;
        //console.log(this.orientation);
    }

    jumpAnimation() {
        if (this.speed_mouvement == Player.STAND);//saut sur place
        else if (this.orientation == Player.MOVE_RIGHT)
            this.sprite.x += (this.speed_mouvement == Player.RUN) ? this.v_x * 2 : this.v_x;
        else if (this.orientation == Player.MOVE_LEFT)
            this.sprite.x -= (this.speed_mouvement == Player.RUN) ? this.v_x * 2 : this.v_x;

        this.sprite.y += this.v_y;
        this.v_y += this.v_grav;

        if (this.sprite.y >= this.original_y) {//si fin de saut
            this.stopJumpAnimation();
        }
    }

    stopJumpAnimation() {
        this.is_jumping = false;
    }

    getPlayerEntity() {
        return this.sprite;
    }

    changeOrientation(orientation) {
        //if (this.is_jumping) return; //pas chg d'orientation 
        if (this.is_dead) return;

        if (orientation == Player.MOVE_LEFT) {
            this.sprite.scaleX = -1;
            this.sprite.x = this.sprite.x + this.sprite.regX;
        } else if (orientation == Player.MOVE_RIGHT) {
            this.sprite.scaleX = 1;
            this.sprite.x = this.sprite.x - this.sprite.regX;
        }
        //console.log(this.sprite.width, this.sprite.regX);
        this.orientation = orientation;
    }

    tick() {
        // si player dead
        if (player.is_dead) {
            (player.getPlayerEntity().alpha <= 0) ? player.getPlayerEntity().alpha = 0 : player.getPlayerEntity().alpha -= 0.02;//effacement du perso 
            this.sprite.y -= Player.WALKING_SPEED_CONFIG;
            return;
        }

        // action for keyboard input
        var want_running = false;
        for (var key in Keyboard.mapping_key_pressed) {
            var requested_action = Keyboard.mapping_touche[key];
            switch (requested_action) {
                case Keyboard.action.dpl_gauche:
                    this.moveLeft();
                    break;
                case Keyboard.action.dpl_droite:
                    this.moveRight();
                    break;
                case Keyboard.action.dpl_saut:
                    this.jump();
                    break;
                case Keyboard.action.courir:
                    want_running = true;
                    break;
            }
        }

        //gestion etat animation : run
        if (this.speed_mouvement != Player.RUN && want_running) this.runAnimation();//si le joueur veut courir
        if (this.speed_mouvement == Player.RUN && !want_running) this.walkAnimation();//si le joueur ne veut plus courir

        //correction d'animation si incoherence
        if (this.speed_mouvement == Player.RUN && this.sprite.currentAnimation != "run")
            this.runAnimation();
        else if (this.speed_mouvement == Player.WALK && this.sprite.currentAnimation != "walk")
            this.walkAnimation();
        else if (this.speed_mouvement == Player.STAND && this.sprite.currentAnimation != "stand")
            this.standAnimation();

        //refresh move anim'
        if (this.is_jumping) this.jumpAnimation();
    }

    moveLeft() {
        this.changeOrientation(Player.MOVE_LEFT);
        if (this.speed_mouvement == Player.STAND) this.speed_mouvement = Player.WALK;

        this.sprite.x -= (this.speed_mouvement == Player.RUN) ? Player.RUNNING_SPEED_CONFIG : Player.WALKING_SPEED_CONFIG;
    }

    moveRight() {
        this.changeOrientation(Player.MOVE_RIGHT);
        if (this.speed_mouvement == Player.STAND) this.speed_mouvement = Player.WALK;

        this.sprite.x += (this.speed_mouvement == Player.RUN) ? Player.RUNNING_SPEED_CONFIG : Player.WALKING_SPEED_CONFIG;
    }
}


/* deprecated - hit box too large
function hitTest(player, monster) {
    var monster_location = monster.getTransformedBounds();
    var monster_width_location = monster_location.x + monster_location.width; 
    var monster_heigth_location = monster_location.y + monster_location.height;
    var player_location = player.getTransformedBounds();
    var player_width_location = player_location.x + player_location.width;
    var player_heigth_location = player_location.y + player_location.height;
    var collision_horizontal = false, collision_vertical = false;

    // collision horizontale
    if ((monster_location.x <= player_location.x) && (player_location.x <= monster_width_location)) collision_horizontal = true;
    else if ((monster_location.x <= player_width_location) && (player_width_location <= monster_width_location)) collision_horizontal = true;

    // collision vertiale
    if ((monster_location.y <= player_location.y) && (player_location.y <= monster_heigth_location)) collision_vertical = true;
    else if ((monster_location.y <= player_heigth_location) && (player_heigth_location <= monster_heigth_location)) collision_vertical = true;

    if (collision_horizontal && collision_vertical)
        return true;
    else
        return false;
}*/